# -*- coding:utf-8 -*-
'''
对word文档进行转换，保存在txt文件；
在转换过程中对中文大写等转换成罗马数字
'''
import docx2txt
import os
import re
import pickle
from collections import OrderedDict
from demo.config import customInfoDir,standardNameflag
from dirHandler import makeDir
from fileHandler import FileOperation
from blockHandler import step2Handler,step3Handler,step4Handler,step5Handler,stepCompanyNameHandler

# pkl_file = open(regionFile, 'rb')
# regionInfo = pickle.load(pkl_file)
# pkl_file.close()
class Word2Txt(object):
    def readWord(self,file):
        '''
        :param file:
        :return:返回word文档中的文本信息，并且对文本信息进行预处理，按行进行分隔
        '''
        # 在对word文档的内容进行处理时实现分行
        text = docx2txt.process(file)   #step-1
        res=[]
        for each in text.split("\n"):   #按照显式进行分行
            lineTxt=each.strip()
            if len(lineTxt)!=0:    #删除空行   step-2
                res.append(lineTxt)
            else:
                continue
        return res

    def countMark(self,context):
        # 后续需要添加表示字符识别
        count=0
        for item in context:
            if self.isIncludetarget(item,u'地址') or self.isIncludetarget(item,u'邮箱') or self.isIncludetarget(item,u'网址') or self.isIncludetarget(item,u'QQ') or self.isIncludetarget(item,u'手机') or self.isIncludetarget(item,u'座机') or self.isIncludetarget(item,u'微信') or self.isIncludetarget(item,u'联系人'):
                count+=1
            else:
                continue
        # 控制一个模块有多少个标准名称
        if count >=2:
            return True
        else:
            return False
    def isIncludetarget(self,string,target):
        if target in string:
            return True
        else:
            return False

    def word2CustomInfoSegmen(self,dirpath,inputfile,parentDirMd5,catalog):
        context=self.readWord(dirpath+inputfile)
        self.customFenkuai(context,catalog,parentDirMd5)

    def customFenkuai(self,content,catalog,parentDirMd5):
        length=len(content)
        targetCount=len(catalog)

        res=OrderedDict()   #每一个自定义标记开始的行数
        i=0
        for ind in xrange(1,targetCount+1):
            flagInfo=catalog[str(ind)]

            res[flagInfo[-1]]=-1
            for j in xrange(i,length):
                if flagInfo[0]==content[j]:
                    res[flagInfo[-1]]=j
                    i=j
                    break
                else:
                    continue
        # sumNum=10000
        #逻辑分块数据行数
        temp=OrderedDict()       #自定义分块信息的开始和结束位置
        ceshi3=res.keys()
        for i in xrange(0,len(ceshi3)):
            key=ceshi3[i]
            if res[key]!=-1:
                temp[key]=[res[key]]
                tmp=temp[key]
                if i< len(ceshi3)-1:
                    for j in xrange(i+1,len(ceshi3)):

                        if res[ceshi3[j]] !=-1:
                            tmp.append(res[ceshi3[j]]-1)
                            temp[key]=tmp
                            break
                        else:
                            if j!=len(ceshi3)-1:
                                continue
                            else:
                                tmp.append(length)
                                temp[key]=tmp
                else:
                    if temp[key]!=-1:
                        tmp.append(length)
                        temp[key]=tmp
            else:
                temp[key]=[]
        # print temp
        # 创建文件夹，并将每一分快信息放入对应文件夹下
        for key,value in temp.items():
            if len(value)==0:
                # 此处为了避免对后续有影响则直接对标识行错误不做任何输出
                continue
            makeDir(customInfoDir+"\\"+parentDirMd5,key)
            fileobj=FileOperation(customInfoDir+"\\"+parentDirMd5+"\\"+key,"")
            fh1=fileobj.fileCreate("fullInfo")
            contentSplice=content[value[0]:value[-1]+1]
            for line in contentSplice:
                fh1.write(line)
                fh1.write("\n")
            fileobj.fileClose(fh1)

        #处理分片信息
        self.customFenPian(customInfoDir+"\\"+parentDirMd5)

    def customFenPian(self,targetDir):
        targetFenkuaiDir=os.listdir(targetDir)
        for oneFenkuai in targetFenkuaiDir:
            #读取分块内容
            content=None
            with open(targetDir+"\\"+oneFenkuai+"\\"+"fullInfo.txt") as f:
                content=f.read()

            content=unicode(content)

            text=step2Handler(content)
            # 1-2中间结果输出
            target1_2=targetDir+"\\"+oneFenkuai+"\\"+"step1-2.txt"
            self.middleResultOutput(target1_2,text)

            text=step3Handler(text)
            # 1-3中间结果输出
            target1_3=targetDir+"\\"+oneFenkuai+"\\"+"step1-3.txt"
            self.middleResultOutput(target1_3,text)

            text=step4Handler(text)
            # 1-4中间结果输出
            target1_4=targetDir+"\\"+oneFenkuai+"\\"+"step1-4.txt"
            self.middleResultOutput(target1_4,text)

            text=step5Handler(text)
            # 1-5中间结果输出
            target1_5=targetDir+"\\"+oneFenkuai+"\\"+"step1-5.txt"
            self.middleResultOutput(target1_5,text)

            fullText=text

            qqContentList=[]
            weichatContentList=[]
            emailContentList=[]
            addressContentList=[]
            phoneContentList=[]
            telContentList=[]
            urlContentList=[]
            specialContentList=[]

            temp=re.findall(standardNameflag,text)
            temp.reverse()

            for each in temp:
                ind=text.rindex(each)

                content=text[ind:]

                text=text[0:ind]
                #地址|邮箱|网址|QQ||手机|座机|微信|特例名
                if each ==u'地址':
                    addressContentList.append(content)
                elif each ==u'邮箱':
                    emailContentList.append(content)
                elif each ==u'网址':
                    urlContentList.append(content)
                elif each ==u'QQ':
                    qqContentList.append(content)
                elif each ==u'手机':
                    phoneContentList.append(content)
                elif each ==u'座机':
                    telContentList.append(content)
                elif each ==u'微信':
                    weichatContentList.append(content)
                else:
                    specialContentList.append(content)

            # 创建文件
            fileObj=FileOperation(targetDir+"\\"+oneFenkuai,oneFenkuai)

            qqFile=fileObj.fileCreate('qq')
            qqContentList.reverse()
            qqFile.write("\n".join(qqContentList))
            weichatFile=fileObj.fileCreate('weichat')
            weichatContentList.reverse()
            weichatFile.write("\n".join(weichatContentList))
            emailFile=fileObj.fileCreate('email')
            emailContentList.reverse()
            emailFile.write("\n".join(emailContentList))
            addressFile=fileObj.fileCreate('address')
            addressContentList.reverse()
            addressFile.write("\n".join(addressContentList))
            phoneFile=fileObj.fileCreate('phone')
            phoneContentList.reverse()
            phoneFile.write('\n'.join(phoneContentList))
            telFile=fileObj.fileCreate('telphone')
            telContentList.reverse()
            telFile.write('\n'.join(telContentList))
            telFile.write("\n")

            urlFile=fileObj.fileCreate('url')
            urlContentList.reverse()
            urlFile.write('\n'.join(urlContentList))
            specialFile=fileObj.fileCreate('special')
            specialContentList.reverse()
            specialFile.write('\n'.join(specialContentList))

            #文件关闭操作
            fileObj.fileClose(qqFile)
            fileObj.fileClose(addressFile)
            fileObj.fileClose(weichatFile)
            fileObj.fileClose(emailFile)
            fileObj.fileClose(phoneFile)
            fileObj.fileClose(telFile)
            fileObj.fileClose(urlFile)
            fileObj.fileClose(specialFile)

            # 对公司名称进行分单元
            self.customCompanyUnit(fullText,fileObj)

    def customCompanyUnit(self,content,fileobj):
        companyNameFile=fileobj.fileCreate('companyName')
        for line in content.split('\n'):
            line=line.strip()
            if len(line)==0:
                continue
            else:
                temp=stepCompanyNameHandler(line)
                if temp:
                    companyNameFile.write(line)
                    companyNameFile.write("\n")
                else:
                    continue
        fileobj.fileClose(companyNameFile)


    def middleResultOutput(self,file,text):
        with open(file,'w') as f:
            f.write(text)
